package com.coin900.coin.cmd.token;

import com.coin900.coin.cmd.ACmd;
import com.coin900.coin.cmd.CmdSign;
import com.coin900.coin.code.CmdCode;
import com.coin900.coin.persist.coin.CoinTokenVO;
import com.coin900.coin.service.coin.ICoinTokenService;
import com.google.gson.Gson;
import com.value.exception.BusinessException;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 删除代币
 *
 * @author shui
 * @create 2018-01-15
 **/
@Controller
public class CoinTokenDelCmd extends ACmd {
    @Override
    public CmdSign execute(HttpServletRequest request, CmdSign sign) throws Exception {
        CmdSign respSign = new CmdSign(sign);
        String source = sign.getSource();
        JSONObject jsonObject = JSONObject.fromObject(source);
        long coinId = jsonObject.getLong("coinId");
        Map<String, Object> params = new HashMap<>();
        params.put("coinId", coinId);
        params.put("dataStatus", 1);
        List<CoinTokenVO> coinTokenVOS = this.tokenService.loadCoinToken(params);
        if (coinTokenVOS.isEmpty()) {
            throw new BusinessException("分类不存在");
        }
        CoinTokenVO coinTokenVO = coinTokenVOS.get(0);
        coinTokenVO.setModifiedInfor(super.getUser(sign));
        coinTokenVO.setDataStatus(0);
        this.tokenService.deleteCoinToken(coinTokenVO);
        return respSign;
    }

    @Autowired
    private ICoinTokenService tokenService;

    @Override
    public String getCmdCode() {
        return CmdCode.COIN_TOKEN_DEL;
    }
}
